//
//  LoginView.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 17.03.25.
//

import SwiftUI

struct LoginView: View {

    @StateObject var viewModel = LoginViewModel()
    
    @EnvironmentObject var appState: AppState
    
    @State var netErrorToast: Bool = false
    @State var isOtherActionShow:Bool = false
    @State var toLoginPhone:Bool = false
    @State var toLoginPassword:Bool = false
    @State var privacyChecked:Bool = false
    @State var isShowPrivacyDialog:Bool = false
    @State var isWXAppInstalled:Bool = false
    @State var loginTitle:String = "登录"
    
    var body: some View {
        NavigationStack {
            ZStack {
                VStack {
                    Text("欢迎使用锦鲤记账")
                        .font(.system(size: 28))
                        .bold()
                        .foregroundColor(Color.white)
                        .padding(EdgeInsets(top: 150, leading: 0, bottom: 10, trailing: 0))
                    Text("每天锦鲤记账，天天好运进账")
                        .font(.system(size: 15))
                        .foregroundColor(Color.white)
                    Spacer()
                    VStack {
                        Button(action:{weichatLogin()}) {
                            Text(loginTitle)
                                .font(.system(size: 16))
                                .bold()
                                .foregroundColor(Color.white)
                                .frame(width: 262, height: 50)
                                .background(Color.koiColorPrimary)
                                .cornerRadius(40)
                        }
                        Button(action: {otherLogin()}) {
                            Text("其它登陆方式")
                                .font(.system(size: 13))
                                .foregroundColor(Color.koiColorTextAssist.opacity(0.7))
                                .padding(EdgeInsets(top: 20, leading: 0, bottom: 30, trailing: 0))
                        }
                        PrivacyCheckView(isChecked: $privacyChecked)
                    }
                }.background(content: {Image("koi_login_background_pure")})
                    .actionSheet(isPresented: $isOtherActionShow){
                        ActionSheet(title: Text(""), buttons: [
                            .default(Text("手机号登陆")) {
                                phoneLogin()
                            },
                            .default(Text("密码登陆")) {
                                passwordLogin()
                            },
                            .default(Text("游客登陆")) {
                                trialLogin()
                            },
                            .cancel()
                        ])
                    }
                if viewModel.isLoading {
                    ZStack {
                        ProgressView()
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(Color.black.opacity(0.2))
                }
            }
        }
        .koiSheet(isPresented: $isShowPrivacyDialog) {
            PrivacyConfirmDialog(
                isShowPrivacyDialog:$isShowPrivacyDialog,
                onConfirmClick: {
                    privacyChecked.toggle()
                    isShowPrivacyDialog.toggle()
                    weichatLogin()
                })
        }
        .fullScreenCover(isPresented: $toLoginPhone, content: {LoginSecondView(viewType: .phone)})
        .fullScreenCover(isPresented: $toLoginPassword, content: {LoginSecondView(viewType: .password)})
        .onAppear{
            isWXAppInstalled = viewModel.isWXAppInstalled()
            if isWXAppInstalled {
                loginTitle = "微信登录"
            } else {
                loginTitle = "手机号登录"
            }
        }
        .onChange(of: viewModel.loginState) { value in
            if value == "success" {
                viewModel.loginState = ""
                appState.isLoggedIn = true
            }
        }
    }
    
    /**
     其它登陆方式
     */
    private func otherLogin() {
        isOtherActionShow.toggle()
    }
    
    /**
     微信登陆
     */
    private func weichatLogin() {
        if !isWXAppInstalled {
            toLoginPhone.toggle()
            return
        }
        if (!checkPrivacy()) {
            return
        }
        viewModel.weichatLogin()
    }
    
    /**
     手机号登陆
     */
    private func phoneLogin() {
        toLoginPhone.toggle()
    }
    
    /**
     密码登陆
     */
    private func passwordLogin() {
        toLoginPassword.toggle()
    }
    
    /**
     游客登陆
     */
    private func trialLogin() {
        print("app state \(appState)")
        viewModel.trialLogin()
    }
    
    private func checkPrivacy() -> Bool {
        if (!privacyChecked) {
            isShowPrivacyDialog.toggle()
        }
        return privacyChecked
    }
}

@available(iOS 18.0, *)
#Preview {
    LoginView()
}
